#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
__author-default__ = 'Pup--->>WX_l599p22'
__cttime-default__ = '20170721'
'''

import sqlite3

def get_conn():
	return sqlite3.connect('test.db')

class User(object):
	def __init__(self,id,name):
		self.id = id
		self.name = name

	def save(self):
		sql = 'insert into user VALUES (?,?);'
		conn = get_conn()
		cur = conn.cursor()
		cur.execute(sql,(self.id,self.name))
		conn.commit()
		cur.close()
		conn.close()

	@staticmethod
	def query():
		sql = 'select * from user'
		conn = get_conn()
		cur = conn.cursor()
		rows = cur.execute(sql)
		users = []
		for row in rows:
			user = User(row[0],row[1])
			users.append(user)
		conn.commit()
		cur.close()
		conn.close()
		return users

	def __str__(self):
		return 'id:{}---name:{}'.format(self.id,self.name)
get_conn()

